# Add a declarative step here for populating the DB with movies.

Given /the following movies exist/ do |movies_table|
  movies_table.hashes.each do |movie|
    Movie.create!(movie)
  end
  assert movies_table.hashes.size == Movie.all.count
end

# Make sure that one string (regexp) occurs before or after another one
#   on the same page

Then /I should see "(.*)" before "(.*)"/ do |e1, e2|
  titles = page.all("table#movies tbody tr td[1]").map {|t| t.text}
  assert titles.index(e1) < titles.index(e2)
end
 
 
 

# Make it easier to express checking or unchecking several boxes at once
#  "When I uncheck the following ratings: PG, G, R"
#  "When I check the following ratings: G"

When /I (un)?check the following ratings: (.*)/ do |uncheck, rating_list|
  rating_list.split(",").each do |field|
    field = field.strip
    if uncheck == "un"
       step %Q{I uncheck "ratings_#{field}"}
       step %Q{the "ratings_#{field}" checkbox should not be checked}
    else
      step %Q{I check "ratings_#{field}"}
      step %Q{the "ratings_#{field}" checkbox should be checked}
    end
  end
end
 
Then /^I should see the following ratings: (.*)/ do |rating_list|
  ratings = page.all("table#movies tbody tr td[2]").map! {|t| t.text}
  rating_list.split(",").each do |field|
    assert ratings.include?(field.strip)
  end
end
 
Then /^I should not see the following ratings: (.*)/ do |rating_list|
  ratings = page.all("table#movies tbody tr td[2]").map! {|t| t.text}
  rating_list.split(",").each do |field|
    assert !ratings.include?(field.strip)
  end
end
 
Then /^I should see all of the movies$/ do
  rows = page.all("table#movies tbody tr td[1]").map! {|t| t.text}
  assert ( rows.size == Movie.all.count )
end
 
#Then /I should see the following ratings unchecked: (.*)/ do |rating_list|
#  rating_list.split(",").each do |field|
#     uncheck("ratings[#{field}]")
#  end
#end

#When /^(?:|I )press the "([^"]*)" button/ do |button|
#  click_button(button)
#end

#Then /I should see only movies with the following ratings: (.*)/ do |rating_list|
#   show_list = []
#   rating_list.split(",").each do |rating|
#      show_list << rating
#   end
#   
#   final_list = show_list.collect { |r| "'" << r << "'" }.join ','
#   movies = Movie.find :all, {:conditions => ["rating in (#{final_list})"]}
#   # puts movies
#end

#And /I should not see movies with the following ratings: (.*)/ do |noshow_list|
#   dont_show_list = []
#   noshow_list.split(",").each do |rating|
#      dont_show_list << rating
#   end
#   final_list = dont_show_list.collect { |r| "'" << r << "'" }.join ','
#   movies = Movie.find :all, {:conditions => ["rating in (#{final_list})"]}
#   # puts movies
#end

#When /I should( not)? see movies with (.*) rating\(s\)/ do |not_seen, rating_list|
#  rating_list.split(",").each do |rating|
#      if not_seen
#        page.should have_no_xpath("//table[@id='movies']//td[.='#{rating}']")
#      else
#        page.should have_xpath("//table[@id='movies']//td[.='#{rating}']")
#      end
#  end
#end

#Given /I have added "(.*)" with rating "(.*)"/ do |title, rating|
#  Given %Q{I am on the Create New Movie page}
#  When  %Q{I fill in "Title" with "#{title}"}
#  And   %Q{I select "#{rating}" from "Rating"}
#  And   %Q{I press "Save Changes"}
#end

#Then /I should see "(.*)" before "(.*)" on (.*)/ do |string1, string2, path|
#  Given %Q{I am on #{path}}
#  regexp = Regexp.new ".*#{string1}.*#{string2}"
#  page.body.should =~ regexp
#end

Then /^I should see no movies$/ do
  rows = page.all("table#movies tbody tr td[1]").map! {|t| t.text}
  assert rows.size == 0
end
#Then /^I should see no movies$/ do#  movies = Movie.find :all, {:conditions => ["rating in ('')"]}
#  # puts movies
#end

#Then /^I should see all movies$/ do
#  movies = Movie.find :all
#  # puts movies
#end

#When /I (un)?check all ratings/ do |unchecked|
#  page.all("form input[type='checkbox']").each do |rating_checkbox|
#    if unchecked
#      page.uncheck rating_checkbox[:name]
#    else
#      page.check rating_checkbox[:name]
#    end
#  end
#end

#When /^I click 'title' header$/ do
#  #pending # express the regexp above with the code you wish you had
#  click_link('Movie Title')
#end

#Then /^I should see movies sorted alphabetically$/ do
#  #pending # express the regexp above with the code you wish you had
#  movies = Movie.all.sort_by {|hash| hash[:title]}
#end

#When /^I click 'release_date' header$/ do
#  #pending # express the regexp above with the code you wish you had
#    click_link('Release Date')
#end

#Then /^I should see movies sorted in increasing order of release date$/ do
#  #pending # express the regexp above with the code you wish you had
#  movies = Movie.all.sort_by {|hash| hash[:release_date]}
#end



